home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Library / Manuels & Misc / Assembly / AOA.ZIP / CH15 / SUBSTR.ASM < prev    next >
Encoding:
Assembly Source File  |  1996-02-25  |  2.0 KB  |  84 lines

  1. ; Substring function.
  2. ;
  3. ; HLL form:
  4. ;
  5. ;procedure substring(var Src:string;
  6. ;             Index, Length:integer;
  7. ;             var Dest:string);
  8. ;
  9. ; Src- Address of a source string.
  10. ; Index- Index into the source string.
  11. ; Length- Length of the substring to extract.
  12. ; Dest- Address of a destination string.
  13. ;
  14. ; Copies the source string from address [Src+index] of length
  15. ; Length to the destination string.
  16. ;
  17. ; If an error occurs, the carry flag is returned set, otherwise
  18. ; clear.
  19. ;
  20. ; Parameters are passed as follows:
  21. ;
  22. ; DS:SI- Source string address.
  23. ; ES:DI- Destination string address.
  24. ; CH- Index into source string.
  25. ; CL- Length of source string.
  26. ;
  27. ; Note: the strings pointed at by the SI and DI registers are
  28. ; length-prefixed strings. That is, the first byte of each 
  29. ; string contains the length of that string.
  30.  
  31. Substring    proc    near
  32.         push    ax
  33.         push    cx
  34.         push    di
  35.         push    si
  36.         clc            ;Assume no error.
  37.         pushf            ;Save direction flag status.
  38.  
  39. ; Check the validity of the parameters.    
  40.  
  41.         cmp    ch, [si]    ;Is index beyond the length of
  42.         ja    ReturnEmpty    ; the source string?
  43.         mov    al, ch        ;See if the sum of index and
  44.         dec    al        ; length is beyond the end of the
  45.         add    al, cl        ; string.
  46.         jc    TooLong        ;Error if > 255.
  47.         cmp    al, [si]    ;Beyond the length of the source?
  48.         jbe    OkaySoFar
  49.  
  50. ; If the substring isnâ•’t completely contained within the source 
  51. ; string, truncate it:
  52.  
  53. TooLong:    popf
  54.         stc            ;Return an error flag.
  55.         pushf
  56.         mov    al, [si]        ;Get maximum length.
  57.         sub    al, ch        ;Subtract index value.
  58.         inc    al        ;Adjust as appropriate.
  59.         mov    cl, al        ;Save as new length.
  60.  
  61. OkaySoFar:    mov    es:[di], cl        ;Save destination string length.
  62.         inc    di
  63.         mov    al, ch        ;Get index into source.
  64.         mov    ch, 0        ;Zero extend length value into CX.
  65.         mov    ah, 0        ;Zero extend index into AX.
  66.         add    si, ax        ;Compute address of substring.
  67.         cld
  68.     rep    movsb            ;Copy the substring.
  69.  
  70.         popf
  71. SubStrDone:    pop    si
  72.         pop    di
  73.         pop    cx
  74.         pop    ax
  75.         ret
  76.  
  77. ; Return an empty string here:
  78.  
  79. ReturnEmpty:    mov    byte ptr es:[di], 0
  80.         popf
  81.         stc
  82.         jmp    SubStrDone
  83. SubString    endp
  84.